var tgd = tgd || {};
tgd.Simulations = tgd.Simulations || {};
tgd.Simulations.Zombies = tgd.Simulations.Zombies || {};

/*
 * opts = { Width, Height, RefreshRate }
 */
tgd.Simulations.Zombies.Simulation = function (opts) {

	this._width = opts.Width;
	this._height = opts.Height;
	this._roundNum = 0;
	this.onRefresh = null;
	this._rr = (opts.RefreshRate ? opts.RefreshRate : 32);
	this._peeps = [];
	this._physics = new tgd.Simulations.Zombies.Physics(this._peeps, this._width, this._height);

	if (tgd.Simulations.Zombies.Simulation._initialized === undefined) {

		tgd.Simulations.Zombies.Simulation.prototype.start = function() {			
			var self = this;

			if (!this._running) {
				self._running = true;
				var simulateRound = function() {
					if (self._running) {
						self._playRound();
						self.onRefresh({ RoundNumber : ++self._roundNum, People : self._peeps /* todo: encapsulate */ });
						setTimeout(simulateRound, self._rr);
					}
				}
				setTimeout(simulateRound, this._rr);
			}
		}

        tgd.Simulations.Zombies.Simulation.prototype.stop = function() {
            this._running = false;
        }

		tgd.Simulations.Zombies.Simulation.prototype.add = function(type) {
			this._peeps.push( { Person : new tgd.Simulations.Zombies.Person(this._physics, type, this._peeps.length), Location : { X : parseInt(Math.random() * 10000) % this._width, Y : parseInt(Math.random() * 10000) % this._height } } );
		}

		tgd.Simulations.Zombies.Simulation.prototype._playRound = function() {
			for (var i = 0; i < this._peeps.length; ++i) {
				this._peeps[i].Person.heartBeat();
			}
        }

		tgd.Simulations.Zombies.Simulation._initialized = true;
	}
}
